package com.uenpay.im.ui.old.activity.nearby;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.qyx.android.weight.utils.baidumap.IGetLocationListener;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.callback.IPopupWindowListener;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @created 2015-12-12 下午3:01:37
 * @copyRight http://www.qiyunxin.com
 * @function 附近的人
 */
public class NearByPeopelActivity extends BaseActivity {

    private ListView listView;
    private View loading_layout;
    private String lLatitude, lLongitude;
    private LinearLayout can_not_location_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_list_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        getLoaction();
    }

    protected void initView() {
        TextView middleText = (TextView) findViewById(R.id.title_textview);
        middleText.setText(R.string.people_nearby);
        listView = (ListView) findViewById(R.id.listview);
        loading_layout = findViewById(R.id.loading);
        can_not_location_layout = (LinearLayout) findViewById(R.id.can_not_location_layout);

        ((TextView) findViewById(R.id.title_right_tv)).setText(R.string.screen);
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
    }

    protected void initListener() {
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        BottomDialogNearBy.showBottomDialog(
                                NearByPeopelActivity.this,
                                new IPopupWindowListener() {

                                    @Override
                                    public void itemClick(int index) {
                                        if (index == 1) {
                                            getData(0);
                                        } else if (index == 2) {
                                            getData(1);
                                        } else if (index == 3) {
                                            getData(-1);
                                        } else if (index == 4) {
                                            clearLocation();
                                        }
                                    }
                                });
                    }
                });

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                QYXUserEntity entity = (QYXUserEntity) arg0.getAdapter().getItem(arg2);
                if (entity != null) {
                    ActivityHandle.startUserDetailActivity(NearByPeopelActivity.this, entity.custid);
                }
            }
        });
    }

    private void getData(int sex) {
        loading_layout.setVisibility(View.VISIBLE);
        GetNearByPeopleInvokeItem item = new GetNearByPeopleInvokeItem(
                lLongitude, lLatitude, sex);
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_ONLY),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                myHandler.sendEmptyMessage(1);
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                myHandler.sendEmptyMessage(1);
                                GetNearByPeopleInvokeItem.GetNearByPeopleInvokeItemResult result = ((GetNearByPeopleInvokeItem) item)
                                        .getOutput();
                                if (result != null && result.status == HttpInvokeResultCode.RESULT_OK) {
                                    NearbyPeopleAdapter adapter = new NearbyPeopleAdapter(
                                            NearByPeopelActivity.this,
                                            result.arrayList);
                                    listView.setAdapter(adapter);
                                }
                            }
                        });

    }

    private void clearLocation() {
        ClearLocationInvokeItem item = new ClearLocationInvokeItem();
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_ONLY),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                ClearLocationInvokeItem.ClearLocationInvokeItemResult result = ((ClearLocationInvokeItem) item)
                                        .getOutput();
                                if (result != null && result.status == HttpInvokeResultCode.RESULT_OK) {
                                    finish();
                                }
                            }
                        });

    }

    /**
     * 定位 获取经纬度
     */
    private void getLoaction() {
        loading_layout.setVisibility(View.VISIBLE);
        IMApplication.getInstance().GetBaiduLoacationUtil()
                .getGoogleLoaction(new IGetLocationListener() {

                    @Override
                    public void onSuccess(BDLocation location) {
                        lLatitude = location.getLatitude() + "";
                        lLongitude = location.getLongitude() + "";
                        if (TextUtils.isEmpty(lLatitude)
                                || TextUtils.isEmpty(lLongitude)) {
                            can_not_location_layout.setVisibility(View.VISIBLE);
                            listView.setVisibility(View.GONE);
                        } else {
                            getData(-1);
                        }
                    }

                    @Override
                    public void onFail() {
                        loading_layout.setVisibility(View.GONE);
                        can_not_location_layout.setVisibility(View.VISIBLE);
                    }
                });
    }

    private Handler myHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 1) {
                if (loading_layout != null) {
                    loading_layout.setVisibility(View.GONE);
                }
            }

            return false;
        }
    });
}
